//
//  MCVistorView.swift
//  HXWeibo
//
//  Created by HX on 15/11/23.
//  Copyright © 2015年 HX. All rights reserved.
//

import UIKit


// 代理协议
protocol MCVistorViewDelegate: NSObjectProtocol
{
    func vistorViewLoginClick()
}

class MCVistorView: UIView
{
    weak var delegate: MCVistorViewDelegate?
    
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        prepareUI()
    }
    
    // MARK: - 按钮点击事件
    func loginClick()
    {
        // 通知代理
        delegate?.vistorViewLoginClick()
    }
    
    // MARK: - 旋转动画
    func startRotationAnimation()
    {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = M_PI * 2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        // 加上这句，不然切换到其他tabBar或退出到桌面，再切回来动画就停止了
        animation.removedOnCompletion = false
        
        // 开始核心动画
        iconView.layer.addAnimation(animation,forKey: "homeRotation")
    }
    
    // MARK: - 核心动画的 开始和暂停
    // MARK: - 暂停旋转
    func pauseAnimation()
    {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画的偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    // MARK: - 恢复旋转
    func resumeAnimation()
    {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度
        iconView.layer.speed = 1
        iconView.layer.timeOffset = 0
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
    }
    
    // MARK: - 设置访客视图的内容
    func setupVistorView(imageName: String, message: String)
    {
        iconView.image = UIImage(named: imageName)
        
        messageLabel.text = message
        messageLabel.sizeToFit()
        
        homeView.hidden = true
        coverView.hidden = true
    }
    // MARK: - 准备UI
    private func prepareUI()
    {
        //添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //: 添加约束
        /// ⚠️要设置约束的view有父控件后才能设置它的自动布局
        // 约束需要添加到view上面，如果不清楚添加到自身还是父控件，可以直接添加到父控件
        
        //: 关闭Autoresizing,不让它干扰autolayout的约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //: 创建约束对象
        // item: 要添加约束的view
        // attribute: 设置的约束
        // relatedBY: Equal
        // toItem: 参照的view
        // attribute: 参照的view的约束
        //: iconView CenterX 与父控件的CenterX重合
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //: iconView CenterY与父控件的CenterY重合
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60))
        
        //: 小房子
        // x
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // y
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //: 消息
        // x
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //y
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //: 宽度约束
        // 当 toItem = nil 时 attribute必须设置为NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
        
        //: 注册按钮
        // 左边和label对齐
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        // 顶部 距离 label底部16
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //宽
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        //高
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //: 登录按钮
        // 右边和label对齐
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        // 顶部距离label底部16
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 宽
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        // 高
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    // MARK: - 懒加载
    /// 转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    /// 小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// 消息Label
    private lazy var messageLabel: UILabel =
    {
        let label = UILabel()
        
        label.text = "关注一些人,看看有什么惊喜"
        
        label.numberOfLines = 0
        
        //:  根据文本适应大小
        label.sizeToFit()
        
        return label
    }()
    /// 注册按钮
    private lazy var registerBtn: UIButton =
    {
        let btn = UIButton()
        
        //: 设置按钮的背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //: 设置文本
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        //: 设置文本颜色
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //: 设置文本大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //: 根据内容适应大小
        btn.sizeToFit()
        
        return btn
    }()
    /// 登录按钮
    private lazy var loginBtn: UIButton =
    {
        let btn = UIButton()
        
        //: 设置背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //: 根据内容适应大小
        btn.sizeToFit()
        
        // 添加点击事件
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    /// 遮罩视图
    private lazy var coverView: UIImageView =
    {
        let coverView = UIImageView()
        
        coverView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return coverView
    }()
}






























